//
//  ATEnterCodeViewController.m
//  Adorable_Things
//
//  Created by ybq on 16/1/19.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "ATEnterCodeViewController.h"
#import "BQNavViewCommon.h"
#import "ATSetPasswordViewController.h"

@interface ATEnterCodeViewController ()
{
    MBProgressHUD *HUD;
}
@property (nonatomic, strong) BQNavViewCommon *navView;
@property (nonatomic, assign) NSInteger num;

@end

@implementation ATEnterCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
}

- (void)createUI {
    _navView = [[BQNavViewCommon alloc] initWithFrame:CGRectMake(0, 20, kWidth, 44)];
    [_navView.backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    _navView.titleLabel.text = @"填写验证码";
    [self.view addSubview:_navView];
    
    _backGroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"LaunchImage-568h"]];
    _nextButton.layer.cornerRadius = 25;
    _revalidationButton.layer.cornerRadius = 14;
    _num = 60;
    _revalidationButton.userInteractionEnabled = NO;
    [_revalidationButton setTitle:[NSString stringWithFormat:@"重发(%.2ld)",_num] forState:UIControlStateNormal];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction:) userInfo:nil repeats:YES];
    [_revalidationButton addTarget:self action:@selector(revalidationButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

- (void)countDownAction:(NSTimer *)timer {
    _num--;
    [_revalidationButton setTitle:[NSString stringWithFormat:@"重发(%.2ld)",_num] forState:(UIControlStateNormal)];
    if (_num == 0) {
        [timer invalidate];
        [_revalidationButton setTitle:[NSString stringWithFormat:@"重新验证"] forState:(UIControlStateNormal)];
        _revalidationButton.userInteractionEnabled = YES;
        _num = 60;
    }
}

- (void)revalidationButtonAction:(UIButton *)button {
    _revalidationButton.userInteractionEnabled = NO;
    [_revalidationButton setTitle:[NSString stringWithFormat:@"重发(%.2ld)",_num] forState:(UIControlStateNormal)];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction:) userInfo:nil repeats:YES];
}

- (void)backButtonAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)nextButtonAction:(id)sender {
    if ([_securityCodeTF.text isEqualToString:_verify_code]) {
        ATSetPasswordViewController *setVC = [[ATSetPasswordViewController alloc] init];
        setVC.phone = _phone;
        setVC.verify_code = _verify_code;
        setVC.myTitle = _myTitle;
        [self.navigationController pushViewController:setVC animated:YES];
    } else {
        [self showHUDWithString:@"您输入的验证码不正确"];
    }
}

#pragma mark -- HUD

- (void)showHUDWithString:(NSString *)string {
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.labelText = string;
    HUD.mode = MBProgressHUDModeText;
    
    //指定距离中心点的X轴和Y轴的偏移量，如果不指定则在屏幕中间显示
    //    HUD.yOffset = 150.0f;
    //    HUD.xOffset = 100.0f;
    
    [HUD showAnimated:YES whileExecutingBlock:^{
        sleep(1);
    } completionBlock:^{
        [HUD removeFromSuperview];
        HUD = nil;
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
